package com.xunyin.modules.storeroom.rest;

import com.alibaba.fastjson.JSONObject;
import com.xunyin.api.ApiResult;
import com.xunyin.api.XunyinException;
import com.xunyin.enums.ErrorEnum;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mainpart.service.TMainPartService;
import com.xunyin.modules.mainpart.vo.TMainPartOnlyNameVo;
import com.xunyin.modules.store.param.StoreQueryParam;
import com.xunyin.modules.store.service.mapper.TStoreMapper;
import com.xunyin.modules.store.vo.TStoreVo;
import com.xunyin.modules.storeroom.service.TStoreRoomService;
import com.xunyin.modules.storeroom.vo.TStoreRoomVo;
import com.xunyin.utils.CommonUtil;
import com.xunyin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 主体门店房间控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-17
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "主体门店房间模块", tags = "共享空间:主体门店房间模块")
public class StoreRoomController {

    private final TStoreRoomService tStoreRoomService;

    private final TMainPartService tMainPartService;

    private final TStoreMapper tStoreMapper;


    /**
     * 首页-根据城市获取房间列表信息
     */
    @AppLog(value = "首页-根据城市获取房间信息", type = 1)
    //@AuthCheck
    @PostMapping("/storeRoom/getInfoTea")
    @ApiOperation(value = "首页-根据城市获取房间信息", notes = "首页-根据城市获取房间信息")
    public ApiResult<List<TStoreVo>> getStoreInfo(@Validated @RequestBody StoreQueryParam param) {
        String cityCode = param.getCityCode();
        if (StringUtils.isEmpty(cityCode)) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        }
        String appId = param.getAppId();
        log.info("getStoreInfo -> appId={}", appId);
        TMainPartOnlyNameVo tMainPartOnlyNameVo = tMainPartService.getMainPartByAppId(appId);
        if (tMainPartOnlyNameVo == null) {
            throw new XunyinException(ErrorEnum.E_90006.getErrorMsg());
        }
        List<TStoreVo> list = tStoreMapper.getStoreInfo(cityCode, tMainPartOnlyNameVo.getId());
        log.info("getStoreInfo -> list={}", list.toString());
        return ApiResult.ok(list);

    }

    /**
     * 根据id获取茶厅（包间）信息
     *
     * @param jsonObject
     * @return
     */
    @AppLog(value = "根据id获取茶厅（包间）信息", type = 1)
    //@AuthCheck
    @PostMapping("/getInfoTeaChild")
    @ApiOperation(value = "首页-根据id获取茶厅（包间）信息", notes = "首页-根据id获取茶厅（包间）信息")
    public JSONObject getInfoTeaChild(@RequestBody JSONObject jsonObject) throws IOException {
        if (jsonObject.getInteger("id") == null) {
            return CommonUtil.errorJson(ErrorEnum.E_90004);
        }
        Integer id = jsonObject.getInteger("id");
        TStoreRoomVo tStoreRoomVo = tStoreRoomService.getInfoTeaChild(id);
        if (tStoreRoomVo == null) {
            return CommonUtil.errorJson(ErrorEnum.E_90004);
        } else {
            return CommonUtil.successJson(tStoreRoomVo);
        }
    }

    /*    *//**
     * 首页-根据id获取茶厅（包间）信息
     *//*
    @AppLog(value = "首页-根据id获取茶厅（包间）信息", type = 1)
    //@AuthCheck
    @PostMapping("/getInfoTeaChild")
    @ApiOperation(value = "首页-根据id获取茶厅（包间）信息",notes = "首页-根据id获取茶厅（包间）信息")
    public ApiResult<TStoreRoom> getInfoTeaChild(@Validated @RequestBody StoreQueryParam param) throws IOException {
        if (param.getId()==null) {
            throw new YshopException(ErrorEnum.E_90004.getErrorMsg());
        }
        TStoreRoom tStoreRoom = tStoreRoomService.getById(param.getId());
        if (tStoreRoom==null){
            throw new YshopException(ErrorEnum.E_90004.getErrorMsg());
        }else {
            return ApiResult.ok(tStoreRoom);
        }
    }*/

}
